//1.创建登陆的模块，并且创建登录的控制器
var loginApp = angular.module('loginApp',[]);
loginApp.controller('loginController',function ($scope,$http) {
    //数据
    $scope.formData={};
    //发送的方法
    $scope.postForm = function () {
        //1.formData添加一个属性，action代表的就是请求的行为
        $scope.formData.action = 'login';
        $http({
            method:'POST',
            url:'./get.php',
            data:$.param($scope.formData),
            headers:{'Content-Type':'application/x-www-form-urlencoded'}
        }).success(function (data) {
            console.log(data);
            if(!data.success){
                //当前登陆失败
                if(!data.errors){
                    //用户名密码错误
                    $scope.message = data.message
                }else{
                    //用户名密码为空的错误
                    $scope.errorUsername = data.errors.username;
                    $scope.errorPassword = data.errors.password;
                }
            }else{
                //当前登录成功
                window.location.href = '#/0';
            }
        })
    }
})
//列表页面的模块
var pageList = angular.module('pageList',[]);
pageList.controller('ListTypeCtrl',function($scope,$http){
    $http({
        method:'GET',
        url:'get.php?action=get_arctype&where=reid=0'
    }).success(function(data){
        console.log(data);
        $scope.lists = data;
    }).error(function(err){
        console.log(err);
    })
})
//文章列表的控制器
totals = 0;
pageList.controller('arcListCtrl',function ($scope,$http,$location) {
    console.log($location);
    $scope.typeid = $location.path().replace('/','')
    if($scope.typeid== 0){
        var get_total_url = 'get.php?action=get_total';
    }else {
        get_total_url = 'get.php?action=get_total&where=typeid='+$scope.typeid;
    }
    $http({
        method:'GET',
        url:get_total_url
    }).success(function (data) {
        $scope.paginationConf.totalItems = data.total;
    }).error(function (err) {
        console.log(err)
    })






    //
    //设置分页的对象
    $scope.paginationConf = {
        currentPage:1,
        itemsPerPage:5,
        pagesLength:5,
        perPageOptions:[10, 20, 30, 40, 50],
        rememberPerPage:'perPageItems',
        onChange:function () {
            if ($scope.paginationConf.currentPage == 1){
                $scope.limit = 0;
            }else {
                $scope.limit = $scope.paginationConf.currentPage * $scope.paginationConf.itemsPerPage
                    - $scope.paginationConf.itemsPerPage;
            }
            if($scope.typeid == 0){
                $geturl = 'get.php?action=get_list&offset=' + $scope.limit + '&rows='
                    + $scope.paginationConf.itemsPerPage + '&orderField=id&orderBy=DESC';
            }else{
                $geturl='get.php?action=get_list&offset='+$scope.limit+'&rows='
                    +$scope.paginationConf.itemsPerPage+'&where=typeid='+$scope.typeid+'&orderField=id&orderBy=DESC';
            }
            // 最后发送前请求
            // offset 当前的查询的偏移量
            // rows 当前查询多少条
            // typeid 查询哪个分类
            // orderField 根据哪个字段进行排序
            // orderBy 排序规则

            $http({
                method:'GET',
                url:$geturl,
            }).success(function(data,status,headers,config){
                $scope.lists = data;
                console.log(data);
            }).error(function(data,status,headers,config){
                console.log('error....')
            })
        }
    }

    //删除模块
    $scope.del = function (index,id) {
        $http({
            method:'GET',
            url:'get.php?action=delete_article&index='+ index + '&id=' + id,
        }).success(function (data) {
            if (data.code==101) {
                //删除成功
                //console.log('删除成功');
                $scope.meg_success="删除成功!";
                $scope.meg_error="";
//                setTimeout(function(){location.href='#/list/0'}, 1000);
                //重新发送ajax请求 页面
                $http({
                    method: 'GET',
                    url: $geturl,
                }).success(function (data) {
                    $scope.lists = data;
                }).error(function (err) {
                })
                // $http({
                //     method: 'GET',
                //     url: get_total_url
                // }).success(function (data) {
                //     $scope.paginationConf.totalItems = data.total;
                // }).error(function (err) {
                //     console.log(err);
                // })

            } else {
                //添加失败
                //console.log('删除失败');
                $scope.meg_error="删除失败";
            }
        })
    }
})
//创建新建模块addCon
var addCont = angular.module('addCont',[]);
addCont.controller('AddContCtrl',function($scope,$http){
    //获取分类的列表
    $http({
        method:'GET',
        url:'get.php?action=get_arctype&where=reid=0'
    }).success(function(data,status,headers,config){
        $scope.lists = data;
    }).error(function(data,status,headers,config){
        console.log('get type is error');
    })
    //执行写入数据的操作
    $scope.formData = {};
    $scope.formData.action = 'add_article';
    $scope.postForm = function(){
        $http({
            method:'POST',
            url:'get.php',
            data: $.param($scope.formData),
            headers:{'Content-Type':'application/x-www-form-urlencoded'}
        }).success(function(data){
            $scope.errorBye = function(){
                $('#errorbox').fadeOut();
            }
            $scope.errorShow = function(){
                $('#errorbox').fadeIn();
            }
            if(!data.errors){
                $scope.meg_success = '插入成功！正在返回列表页面..';
                $scope.meg_error = '';
                setTimeout(function(){location.href='#/0'},1000);
            }else{
                //添加失败
                $scope.meg_success = '';
                var get_error = '';
                if(data.errors.hasOwnProperty('title')){
                    get_error=data.errors.title;
                }
                if(data.errors.hasOwnProperty('content')){
                    get_error=get_error+(get_error?"&":"")+data.errors.content;
                }
                if(data.errors.hasOwnProperty('typeid')){
                    get_error=get_error+(get_error?"&":"")+data.errors.typeid;
                }
                $scope.meg_error=get_error;

            }
        })
    }
})
var modifyCont = angular.module('modifyCont',[]);
modifyCont.controller('ModifyContCtrl',function($scope,$http,$stateParams){
    //获取分类列表
    $http({
        method: 'GET',
        url: 'get.php?action=get_arctype&where=reid=0'
    }).success(function(data, status, headers, config) {
        $scope.types=data;
    }).error(function(data, status, headers, config) {
        console.log("get type list error...");
    });
    //读取这一条数据
    console.log($stateParams);
    $http({
        method: 'GET',
        url: 'get.php?action=get_article&id='+$stateParams.Id
    }).success(function(data, status, headers, config) {
        $scope.lists=data;
    }).error(function(data, status, headers, config) {
        console.log("error...");
    });
    //更新数据
    $scope.formData = {};
    $scope.postForm = function() {
        $scope.formData.action = 'update_article';
        $scope.formData.id = $stateParams.Id;
        $scope.formData.title = form.title.value;
        $scope.formData.content = form.content.value;
        $scope.formData.typeid = $("#typeid option:selected").val();//待优化取值方式
        $http({
            method  : 'POST',
            url     : 'get.php',
            data    : $.param($scope.formData),
            headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
        })
            .success(function(data) {
                console.log(data);
                if (data.code==101) {
                    //添加成功
                    console.log('修改成功');
                    $scope.meg_success="修改成功! 正在返回列表页...";
                    $scope.meg_error="";
                    setTimeout(function(){location.href='#/0'}, 1000);
                } else {
                    //添加失败
                    console.log('修改失败');
                    var get_errors="";
                    $scope.meg_success="";
                    //信息提示框状态
                    $scope.errorBye=function(){
                        $('#errorbox').fadeOut();
                    }
                    $scope.errorShow=function(){
                        $('#errorbox').fadeIn();
                        $scope.meg_error='';
                    }
                    if(data.errors){
                        console.log("有错误信息");
                        if(data.errors.hasOwnProperty('title')){
                            get_errors=data.errors.title;
                        }
                        if(data.errors.hasOwnProperty('content')){
                            get_errors=get_errors+(get_errors?"&":"")+data.errors.content;
                        }
                        $scope.meg_error=get_errors;
                    }else{
                        console.log("无错误信息");
                        $scope.meg_error="修改失败，无改动！";
                    }
                }
            });
    };
})

//详情模块
var showCont = angular.module("showCont", []);
showCont.controller('ShowContCtrl', function($scope, $http, $stateParams) {
    console.log($stateParams.Id);
    $http({
        method: 'GET',
        url: 'get.php?action=get_article&id='+$stateParams.Id
    }).success(function(data, status, headers, config) {
        console.log("success...");
        console.log(data);
        $scope.lists=data;
    }).error(function(data, status, headers, config) {
        console.log("error...");
    });
});
//
// var delCont = angular.module("delCont", []);
// delCont.controller('delContCtrl', function($scope, $http, $stateParams) {
//     $scope.formData = {};
//     $scope.del=function (index,id) {
//        $scope.formData.id=id;
//         $scope.formData.action='delete_article'
//
//         $http({
//             method:'POST',
//             url:'get.php',
//             data   : $.param($scope.formData),
//             headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
//         }).success(function (data) {
//             if(data.code==101){
//                 console.log(1111)
//                 // setTimeout(function(){location.href='#/0'}, 1000);
//                $scope.meg_success="删除成功";
//                 $scope.meg_error="";
//             }else{
//
//             }
//
//         }).error(function (err) {
//             console.log(err)
//         })
//     }
// })






